<?php

namespace App\Task;

use App\Model\UserCharmRichLog;
use App\Model\UserRankDay;
use App\Model\UserRankMon;
use App\Model\UserRankTotal;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\DbConnection\Db;

/**
 * 定时计算排行榜
 * @Crontab(name="CronRankTask", rule="11 11 * * *", callback="execute", memo="定时计算排行榜", singleton=true)
 */
class CronRankTask
{
    public $limit = 20;

    /**
     * @return void
     */
    public function execute()
    {
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        //日榜
        $this->dailyRank();
        //月榜
        $this->monthRank();
        //总榜
        $this->totalRank();
        log_info(__CLASS__ . "==End==" . date('Y-m-d H:i:s'));
    }

    /**
     * 日榜
     * php bin/hyperf.php cron dailyRank 1
     */
    function dailyRank()
    {
        // $date = $start = date('Y-m-d');
        // $end = date('Y-m-d', strtotime('+1 day'));
        $date = date('Y-m-d');
        $start = date('Y-m-d', strtotime('-30 day'));
        $end = date('Y-m-d', strtotime('+1 day'));

        //清除记录
        $sql = "TRUNCATE t_user_rank_day";
        Db::statement($sql);

        //魅力榜
        $where = [
            ['created_at', '<', $end],
            ['created_at', '>', $start],
            ['type', 'charm']
        ];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        foreach ($items as $idx => $item) {
            if ($idx >= $this->limit) break;
            $rank = $idx + 1;
            UserRankDay::addCharmRecord($item['user_id'], $rank, $item['value'], $date);
            log_info(__CLASS__ . "==dailyRank==UserId:{$item['user_id']}==Charm:{$item['value']}==Rank:{$rank}");
        }

        //豪气榜
        $where = [
            ['created_at', '<', $end],
            ['created_at', '>', $start],
            ['type', 'rich']
        ];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        foreach ($items as $idx => $item) {
            if ($idx >= $this->limit) break;
            UserRankDay::addRichRecord($item['user_id'], $idx + 1, $item['value'], $date);
            log_info(__CLASS__ . "==dailyRank==UserId:{$item['user_id']}==Rich:{$item['value']}==Rank:{$rank}");
        }
        return;
    }

    /**
     * 月榜
     * php bin/hyperf.php cron monthRank 1
     */
    function monthRank()
    {
        $start = date('Y-m-d', strtotime('-30 day'));
        $end = date('Y-m-d', strtotime('+1 day'));

        //清除记录
        $sql = "TRUNCATE t_user_rank_mon";
        Db::statement($sql);

        //魅力榜
        $where = [
            ['created_at', '<', $end],
            ['created_at', '>', $start],
            ['type', 'charm']
        ];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        foreach ($items as $idx => $item) {
            if ($idx >= $this->limit) break;
            $rank = $idx + 1;
            UserRankMon::addCharmRecord($item['user_id'], $rank, $item['value']);
            log_info(__CLASS__ . "==monthRank==UserId:{$item['user_id']}==Charm:{$item['value']}==Rank:{$rank}");
        }

        //豪气榜
        $where = [
            ['created_at', '<', $end],
            ['created_at', '>', $start],
            ['type', 'rich']
        ];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        foreach ($items as $idx => $item) {
            if ($idx >= $this->limit) break;
            UserRankMon::addRichRecord($item['user_id'], $idx + 1, $item['value']);
            log_info(__CLASS__ . "==monthRank==UserId:{$item['user_id']}==Rich:{$item['value']}==Rank:{$rank}");
        }
        return;
    }

    /**
     * 总榜
     * php bin/hyperf.php cron totalRank 1
     */
    function totalRank()
    {
        //清除记录
        $sql = "TRUNCATE t_user_rank_total";
        Db::statement($sql);

        //魅力榜
        $where = [['type', 'charm'], ['flag', 1]];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);

        foreach ($items as $idx => $item) {
            if ($idx >= $this->limit) break;
            $rank = $idx + 1;
            UserRankTotal::addCharmRecord($item['user_id'], $rank, $item['value']);
            log_info(__CLASS__ . "==totalRank==UserId:{$item['user_id']}==Charm:{$item['value']}==Rank:{$rank}");
        }

        //豪气榜
        $where = [['type', 'rich'], ['flag', 1]];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        foreach ($items as $idx => $item) {
            if ($idx >= $this->limit) break;
            UserRankTotal::addRichRecord($item['user_id'], $idx + 1, $item['value']);
            log_info(__CLASS__ . "==totalRank==UserId:{$item['user_id']}==Rich:{$item['value']}==Rank:{$rank}");
        }
        return;
    }
}
